# 保存为locustfile.py
# coding=utf-8
from locust import HttpUser, TaskSet, task
from selenium import webdriver
import time
import os
import string
import unittest, random, sys

'''
实现场景：先登录（只登录一次），然后访问->我的地盘页->产品页->项目页
访问我的地盘页面权重为2，产品页和项目页权重各为1
***作者：上海-悠悠 QQ群：588402570**
'''

class UserBehavior(TaskSet):

    cookiejar = {}
    '''蝗虫行为类'''
    def _login(self):
        driver = webdriver.phantomjs()
        driver.get("/login")
        time.sleep(2)
        u = driver.find_element_by_id("_username")
        time.sleep(2)
        u.clear()
        u.send_keys('cj_001')
        p = driver.find_element_by_id("_password")
        time.sleep(3)
        p.clear()
        p.send_key('123456')
        # 定位单位元素
        unit_ck = driver.find_element_by_xpath("//span[@class='select2-chosen']")
        time.sleep(2)
        unit_ck.find_element_by_xpath("//div[contains(text(),'cj_001')]")
        time.sleep(2)
        # 定位登陆按钮
        submit_button = driver.find_element_by_xpath("//div[@id='computerLogin']//input[@class='login_submit_btn']")
        submit_button.click()

        for cookie in driver.get_cookies():
            # self.cookiejar[cookie['rcm.session.id']] = cookie['value']
            self.cookiejar[cookie['accountSuitId']] = cookie['value']



    def on_start(self):

        '''任务开始准备工作：只登录一次'''
        self._login()

    # 任务1-事前申请
    @task(2)
    def rcm_advance_apply(self):
        print("---访问事前申请页面---")

        num = ''.join(random.sample(string.digits, 8))
        driver = webdriver.phantomjs()
        self.client.request('get', "/budexec/beforeApplyCommon/form", cookie=self.cookiejar)

        # 事前申请页面信息添加
        # 预算指标添加
        apply_zb = driver.find_element_by_xpath("//input[@id='budgetName']")
        driver.switch_to.frame(apply_zb.find_element_by_xpath("//iframe[@id='jbox-iframe']"))
        apply_zb01 = apply_zb.find_element_by_xpath("//tr[4]//td[1]//input[1]")
        driver.switch_to.default_content()
        apply_button = driver.find_element_by_xpath("//button[@class='jbox-button-qr jbox-button-focus']")
        apply_button.click()

        # 签订合同
        apply_click01 =driver.find_element_by_xpath("// input[ @ id = 'contractFlag2']")
        apply_click01.click()
        # 申请事由xpath=//textarea[@id='applyReason']
        apply_reason = driver.find_element_by_xpath("//textarea[@id='applyReason']")
        apply_reason.text("ces20200603001")
        # 申请金额xpath=//input[@id='applyAmount0']
        apply_amount = driver.find_element_by_xpath("//input[@id='applyAmount0']")
        apply_amount.text("12.5")
        # 事前申请附件上传

        # 事前申请附件预览

        # 提交xpath=//body/form/div/input[2]
        apply_check_button = driver.find_element_by_xpath("//body/form/div/input[2]")
        apply_check_button.click()













    # 任务2-附件上传
    @task(1)
    def zentao_product(self):
        print("---附件上传---")


    # 任务3-附件预览
    @task(1)
    def zentao_prject(self):
        print("---附件预览---")


class WebsiteUser(HttpUser):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 1000

if __name__ == "__main__":

    os.system("locust -f locustfile.py --host=http://192.168.1.144/rcm/a")